package com.bestvike.stone.spring.client;

import org.springframework.http.HttpEntity;
import org.springframework.util.MultiValueMap;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import java.util.Objects;

/**
 * Created by 许崇雷 on 2019-02-21.
 */
public final class HttpsEntity<T> extends HttpEntity<T> {
    private final HostnameVerifier hostnameVerifier;
    private final SSLSocketFactory socketFactory;

    public HttpsEntity(T body, MultiValueMap<String, String> headers, HostnameVerifier hostnameVerifier, SSLSocketFactory socketFactory) {
        super(body, headers);
        this.hostnameVerifier = hostnameVerifier;
        this.socketFactory = socketFactory;
    }

    public HostnameVerifier getHostnameVerifier() {
        return this.hostnameVerifier;
    }

    public SSLSocketFactory getSocketFactory() {
        return this.socketFactory;
    }

    public boolean hasSslOptions() {
        return this.hostnameVerifier != null || this.socketFactory != null;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null || this.getClass() != obj.getClass())
            return false;
        if (!super.equals(obj))
            return false;
        HttpsEntity<?> that = (HttpsEntity<?>) obj;
        return Objects.equals(this.hostnameVerifier, that.hostnameVerifier)
                && Objects.equals(this.socketFactory, that.socketFactory);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), this.hostnameVerifier, this.socketFactory);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("<");
        if (this.hostnameVerifier != null) {
            builder.append(this.hostnameVerifier);
            builder.append(',');
        }
        if (this.socketFactory != null) {
            builder.append(this.socketFactory);
            builder.append(',');
        }
        if (this.getBody() != null) {
            builder.append(this.getBody());
            builder.append(',');
        }
        builder.append(this.getHeaders());
        builder.append('>');
        return builder.toString();
    }
}
